package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.Dict;
import com.exam.demo.entity.Menu;
import com.exam.demo.service.DictService;
import com.exam.demo.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    MenuService menuService;

    @Resource
    DictService dictService;

    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name){

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        Page<Menu> page = menuService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
    //查找根据id全部专业数据
    @GetMapping("/selectByCollegeId/{college_id}")
    public Result findPage(@PathVariable String college_id){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        List<Menu> list = menuService.list(queryWrapper.eq("college_id",college_id));
        return Result.success(list);
    }


    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Menu menu){
        return Result.success(menuService.saveOrUpdate(menu));
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(menuService.removeById(id));
    }

    //查找全部数据
    @GetMapping("/findAll")
    public Result findAll(@RequestParam(defaultValue = "") String name){
        return Result.success(menuService.findMenus(name));
    }
    //查出数据库所有ids
    @GetMapping("/ids")
    public Result findAllIds() {
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    //查找全部数据
    @GetMapping("/icons")
    public Result getIcons(){
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type","icon");
        return Result.success(dictService.list(dictQueryWrapper));
    }


}
